package com.example.dreamlist2.friend;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.dreamlist2.R;
import com.example.dreamlist2.StateInfo;
import com.example.dreamlist2.goal.HabitAdapter;
import com.example.dreamlist2.goal.HabitAddActivity;
import com.example.dreamlist2.goal.HabitCompleteActivity;
import com.example.dreamlist2.goal.HabitCompleteItem;
import com.example.dreamlist2.goal.HabitItem;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class FriendFragment extends Fragment {

    private boolean isGetData = false;
    protected Activity mActivity;
    //StateInfo stateInfo = (StateInfo)(requireActivity().getApplication());

    private List<HabitCompleteItem> recordList = new ArrayList<>();

    StateInfo stateInfo;

    String uid;

    private HabitListAdapter adapter;
    RecyclerView recyclerView;


    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        //   进入当前Fragment
        if (enter && !isGetData) {
            isGetData = true;
            //   这里可以做网络请求或者需要的数据刷新操作
            Bundle bundle = getArguments();
            assert bundle != null;
            uid = bundle.getString("uid");
            //展示所有打卡记录
            search_record(uid);
        } else {
            isGetData = false;
        }
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onAttach(@NotNull Context context) {
        this.mActivity = (Activity)context;
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.record_list, container, false);

        recyclerView = view.findViewById(R.id.attractions_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

        stateInfo = (StateInfo) mActivity.getApplication();

        Bundle bundle = getArguments();
        assert bundle != null;
        uid = bundle.getString("uid");

        //展示所有打卡事项
        search_record(uid);




        return view;
    }

    public void search_record(String uid){
        BmobQuery<HabitCompleteItem> query= new BmobQuery<HabitCompleteItem>();
        query.addWhereEqualTo("uid", uid);
        query.findObjects(new FindListener<HabitCompleteItem>() {
            @Override
            public void done(List<HabitCompleteItem> list, BmobException e) {
                if (e == null) {
                    //Toast.makeText(mActivity, "成功,共" + list.size() + "条数据", Toast.LENGTH_LONG).show();
                    Log.i("record","查询成功"+uid+" "+list.size());
                    recordList = list;
                    adapter = new HabitListAdapter(recordList);
                    //recyclerView.setLayoutManager(new LinearLayoutManager(this));
                    recyclerView.setAdapter(adapter);
                }
                else {
                    Log.i("record","查询不成功");
                }
            }
        });


    }


}